package com.testdemo.springboot_demo.service;


import com.testdemo.springboot_demo.pojo.User;
import com.testdemo.springboot_demo.pojo.dto.UserDTO;
import com.testdemo.springboot_demo.repository.UserRepository;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

@Service
public class UserService implements InterfaceUserService {

    @Autowired
    UserRepository userRepository;

    @Override
    public User add(UserDTO user) {
        User userPojo=new User();

        BeanUtils.copyProperties(user,userPojo);

        return userRepository.save(userPojo);
        //调用数据访问类
    }

    @Override
    public User getUser(Integer userId) {
        return userRepository.findById(userId).orElseThrow(()->{
            throw new IllegalArgumentException("用户不存在,参数异常");
        });
    }

    @Override
    public User edit(Integer userId, UserDTO user) {
        User userPojo=new User();
        userPojo.setUserID(userId);
        BeanUtils.copyProperties(user,userPojo);
        return userRepository.save(userPojo);
    }



    @Override
    public void delete(Integer userId) {
         userRepository.deleteById(userId);
    }
}
